package com.itbour.web.action.authority;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.authority.BackFuncDto;
import com.itbour.dto.authority.FuncIds;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.service.authority.BackFuncInfoService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 后台功能表ACTION
 * @author	 LQ
 */
public class GetBackFuncInfoWebAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	private BackFuncInfoService backFuncInfoService;
	private BackFuncDto backFuncDto;
	private HashMap<String, Object> mapResult=new HashMap<String, Object>();
	private List<BackFuncDto> listResult;
	private String findDesc;// 通过描述内容查找
	private String findFuncTypeL;//func_parent
	private String findFuncTypeM;//func_type 功能区分 10：后台管理人员 20：设计师
	private String findFuncTypeS;//区分是否有父类 0:不限 ----1:有父类 ---2：无父类
	
	/**功能表分页获取
	 * @return mapResult
	 */
	public String pageListBackFuncInfo() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		if (user != null && user.getUser_id() != null) {
			Map<String, Object> parm = new HashMap<String, Object>();
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer pageNum = Integer.parseInt(request.getParameter("page"));
			Integer pageSize = Integer.parseInt(request.getParameter("rows"));
			if (StringUtils.isNotEmpty(findDesc)) {
				parm.put("desc", findDesc);
			}
			if (StringUtils.isNotEmpty(findFuncTypeM)) {
				parm.put("func_type", findFuncTypeM);
			}
			if(StringUtils.isNotEmpty(findFuncTypeS)){
				if(Integer.parseInt(findFuncTypeS)!=0){
					//定义Parent=-1和0 区分查看 有无父类
					if(Integer.parseInt(findFuncTypeS)==1){
						parm.put("parent","-1");//表示 有父类
						if (StringUtils.isNotEmpty(findFuncTypeL)) {
							if(Integer.parseInt(findFuncTypeL)==0){
								parm.put("parent", null);
							}else{
								parm.put("parent", findFuncTypeL);
							}
						}
					}
					if(Integer.parseInt(findFuncTypeS)==2){//查看无父类
						parm.put("parent","0");
					}
				}else{
					if (StringUtils.isNotEmpty(findFuncTypeL)) {
						if(Integer.parseInt(findFuncTypeL)==0){
							parm.put("parent", null);
						}else{
							parm.put("parent", findFuncTypeL);
						}
					}
				}
			}
			Page page=backFuncInfoService.pageBackFuncInfo(pageNum, pageSize, parm);
			mapResult.put("total", page.getTotalCount());
			mapResult.put("rows", page.getResult());
		}
		return SUCCESS;
	}

	
	/**
	 * 添加功能表信息
	 * 
	 * @throws Exception
	 */
	public void addBackFuncInfo() throws Exception {
		if (backFuncDto != null) {
			Map<String, Object> session = ActionContext.getContext().getSession();
			UserInfoDto user = (UserInfoDto) session.get("user");
			if (user != null && user.getUser_id() != null) {
				backFuncDto.setUpdate_by(user.getUser_id().toString());
				if (backFuncDto.getFunc_id()!= null) {
					this.updBackFuncInfo();//修改信息
				} else {
					backFuncInfoService.addBackFuncInfo(backFuncDto);
					this.writeJSON("ok");
				}
			}else{
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("no");
		}
	}
	
	/**修改后台功能表信息
	 * @throws Exception
	 */
	public void updBackFuncInfo() throws Exception{
		if (backFuncDto != null) {
			Map<String, Object> session = ActionContext.getContext().getSession();
			UserInfoDto user = (UserInfoDto) session.get("user");
			if (user != null && user.getUser_id() != null) {
				backFuncDto.setUpdate_by(user.getUser_id().toString());
				backFuncInfoService.updBackFuncInfo(backFuncDto);
				this.writeJSON("ok");
			} else {
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("no");
		}
	}
	
	/**获取单个功能信息
	 */
	public String getOneBackFuncInfo(){
		HttpServletRequest request=ServletActionContext.getRequest();
		Integer id=Integer.parseInt(request.getParameter("id"));
		if(id!=null){
			Map<String, Object> parm=new HashMap<String, Object>();
			parm.put("func_id", id);
			listResult=backFuncInfoService.selectOneBackFuncInfo(parm);
		}
		return SUCCESS;
	}
	
	/**删除功能表信息
	 * 增加父类节点    如果删除某个func_id 则删除该ID下的所有子类！
	 * @throws Exception 
	 */
	public void delBackFuncInfo() throws Exception{
		HttpServletRequest request=ServletActionContext.getRequest();
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		String ids=request.getParameter("ids");
		if(ids!=null&&user.getUser_id()!=null&&ids.length()>0){
			Map<String, Object> parm=new HashMap<String, Object>();
			parm.put("ids", ids);
			parm.put("parent", ids);
			parm.put("update_by", user.getUser_id());
			backFuncInfoService.delBackFuncInfo(parm);
			this.writeJSON("ok");
		}else{
			this.writeJSON("no");
		}
	}
	
	/***获取父功能信息
	 **地址请求difId=1或者2 用于区分场合请求
	 **difId=1表示(查询)父类请求接口
	 **difId=2表示新建和修改位置的父类请求接口
	 **funcId 新建或者修改not in funcId
	 */
	public String getParentInfo(){
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		HttpServletRequest request=ServletActionContext.getRequest();
		Integer difId=Integer.parseInt(request.getParameter("difId"));
		if(user!=null){
			Map<String, Object> parm=new HashMap<String, Object>();
			if(difId==2){
				Integer funcId=Integer.parseInt(request.getParameter("funcId"));
				if(funcId!=null&&funcId>0){
					parm.put("func_id", funcId);
				}
			}else{
				Set<Long> parentId=backFuncInfoService.getFatherIds(parm);
				List<Long> fatherIds=new ArrayList<Long>(parentId);
				fatherIds.remove(null);
				parm.put("parentIds", StringUtils.listToString2(fatherIds));	
			}
			listResult=backFuncInfoService.getParentInfo(parm);
			BackFuncDto b=new BackFuncDto();
			Long fd=(long)0;
			b.setFunc_id(fd);
			b.setName("无父类");
			b.setParent(null);
			BackFuncDto bb=new BackFuncDto();
			Long fdd=(long)-1;
			bb.setFunc_id(fdd);
			bb.setName("不限");
			bb.setParent(null);
			if(difId==1){
				listResult.add(0,bb);
			}else{
				listResult.add(0,b);
			}
			
		}
		return SUCCESS;
	}


	public BackFuncInfoService getBackFuncInfoService() {
		return backFuncInfoService;
	}
	public void setBackFuncInfoService(BackFuncInfoService backFuncInfoService) {
		this.backFuncInfoService = backFuncInfoService;
	}
	public BackFuncDto getBackFuncDto() {
		return backFuncDto;
	}
	public void setBackFuncDto(BackFuncDto backFuncDto) {
		this.backFuncDto = backFuncDto;
	}
	public HashMap<String, Object> getMapResult() {
		return mapResult;
	}
	public void setMapResult(HashMap<String, Object> mapResult) {
		this.mapResult = mapResult;
	}
	public List<BackFuncDto> getListResult() {
		return listResult;
	}


	public void setListResult(List<BackFuncDto> listResult) {
		this.listResult = listResult;
	}


	public String getFindDesc() {
		return findDesc;
	}


	public void setFindDesc(String findDesc) {
		this.findDesc = findDesc;
	}


	public String getFindFuncTypeL() {
		return findFuncTypeL;
	}


	public void setFindFuncTypeL(String findFuncTypeL) {
		this.findFuncTypeL = findFuncTypeL;
	}


	public String getFindFuncTypeM() {
		return findFuncTypeM;
	}
	public void setFindFuncTypeM(String findFuncTypeM) {
		this.findFuncTypeM = findFuncTypeM;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public String getFindFuncTypeS() {
		return findFuncTypeS;
	}
	public void setFindFuncTypeS(String findFuncTypeS) {
		this.findFuncTypeS = findFuncTypeS;
	}
}
